package com.eight.travel.controller;

import com.eight.travel.pojo.User;
import com.eight.travel.service.UserService;
import com.eight.travel.utils.BaseResp;
import com.eight.travel.utils.JWTUtils;
import com.eight.travel.utils.UploadPic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Q小旭
 * @version 1.0
 */
@RestController
@Api(value = "通用接口",tags = {"通用接口"})
@RequestMapping("/user")
@CrossOrigin(origins = "*",allowCredentials = "true")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public BaseResp login(@RequestBody User user,HttpServletRequest request){
        ServletContext servletContext = request.getServletContext();
        return userService.login(user,servletContext);
    }

    @ApiOperation(value = "JWT验证登录")
    @RequestMapping("/verfiyToken")
    public BaseResp verfiyToken(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String token = null;
        if (cookies==null || cookies.length==0){
            return new BaseResp().FAIL("用户未注册");
        }
        for (Cookie co:cookies){
            if (co.getName().equals("token")){
                token=co.getValue();
            }
        }
        if (StringUtils.isEmpty(token)){
            return new BaseResp().FAIL("未登录");
        }
        //使用jwt解析token
        Map map = JWTUtils.verfiyJWT(token);
        if (map==null){
            return new BaseResp().FAIL("登录信息有误！");
        }
        return new BaseResp().SUCCESS("登录成功!",null,null);
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public BaseResp register(@RequestBody User user){
        return userService.register(user);
    }

    @ApiOperation(value = "上传头像")
    @PostMapping("/upload")
    public BaseResp upload(MultipartFile file){
        String upload = UploadPic.upload(file);
        return new BaseResp().SUCCESS("上传成功",upload,null);
    }

    @ApiOperation(value = "发送邮件")
    @GetMapping ("/mail/{mail}")
    public BaseResp sendMail(@PathVariable("mail") String mail,HttpServletRequest request) throws MessagingException {
        System.out.println("mail = " + mail);
        BaseResp baseResp = userService.sendMail(mail,request);
        return baseResp;
    }

    @ApiOperation(value = "分页查询用户")
    @GetMapping("/findAll")
    public BaseResp findAll(Integer page,Integer size){
        return userService.findAll(page,size);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping ("/deleteById")
    public BaseResp deleteById(@RequestBody User user ){
        return userService.deleteById(user);
    }

    @ApiOperation(value = "编辑用户")
    @PostMapping("/addOrUpdate")
    public BaseResp addOrUpdate(@RequestBody User user ){
        boolean b = userService.saveOrUpdate(user);
        if (b == true){
            return new BaseResp().SUCCESS("操作成功",null,null);
        }
        return new BaseResp().FAIL("操作失败");
    }

}
